﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Hisun.Models;

namespace Hisun.View.DataExchange
{
    public partial class WebTest : System.Web.UI.Page
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                int Totals = TotalPageAmount();
                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1页，转到";
                BindMyData(0);

            }
        }


        private void BindMyData(int PageIndex)
        {

            int skipAccount = PageIndex * 10;

            //List<VABC_NC_StockInSumCost> Query1 = new List<VABC_NC_StockInSumCost>();
            //List<VABC_NC_StockInSumCost> Query2 = new List<VABC_NC_StockInSumCost>();
            List<VABC_NC_StockIn汇总> Query1 = new List<VABC_NC_StockIn汇总>();
            List<VABC_NC_StockIn汇总> Query2 = new List<VABC_NC_StockIn汇总>();
            List<VABC_NC_StockIn汇总> Query3 = new List<VABC_NC_StockIn汇总>();

            string QueryValue = TextBox_QueryValue.Text;

            //var Query1=from a in db.VABC_NC_StockInSumCost
            //              select a;

            if (QueryValue == "")
            {
               
                Query1 = (from a in db.VABC_NC_StockIn汇总
                          select a).ToList();
            }
            else
            {

                Query1 = (from a in db.VABC_NC_StockIn汇总
                          where a.存货编码.Contains(QueryValue)
                          select a).ToList();
  
            }
            if (CheckBox_Error.Checked == true)
            {
                Query2 = (from a in Query1
                          where a.NC数量 != a.ABC数量
                          select a).ToList();
                          
            }
            else
            {
                Query2 = (from a in Query1
                          where a.NC数量 == a.ABC数量
                          select a).ToList();
            }
            if (CheckBox_Code.Checked == true)
            {
                Query3 = (from a in Query2
                          where a.NC数量 ==0 || a.ABC数量==0
                          select a).ToList();

            }
            else
            {
                Query3 = (from a in Query2
                          where a.NC数量 != 0 && a.ABC数量 != 0
                          select a).ToList();
            }
            var MyQuery=(from a in Query3
                         select a).ToList().Skip(skipAccount).Take(10);
            GridView_1.DataSource = MyQuery;
            GridView_1.DataBind();


        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;

            List<VABC_NC_StockIn汇总> Query1 = new List<VABC_NC_StockIn汇总>();
            List<VABC_NC_StockIn汇总> Query2 = new List<VABC_NC_StockIn汇总>();
            List<VABC_NC_StockIn汇总> Query3 = new List<VABC_NC_StockIn汇总>();

            string QueryValue = TextBox_QueryValue.Text;

            if (QueryValue == "")
            {

                Query1 = (from a in db.VABC_NC_StockIn汇总
                          select a).ToList();
      
            }
            else
            {


                Query1 = (from a in db.VABC_NC_StockIn汇总
                          where a.存货编码.Contains(QueryValue)
                          select a).ToList();

            }
            if (CheckBox_Error.Checked == true)
            {
                Query2 = (from a in Query1
                          where a.ABC数量!=a.NC数量
                          select a).ToList();
            }
            else
            {
                Query2 = (from a in Query1
                          where a.ABC数量 == a.NC数量
                          select a).ToList();
            }
            if (CheckBox_Code.Checked == true)
            {
                Query3 = (from a in Query2
                          where a.NC数量 == 0 || a.ABC数量 == 0
                          select a).ToList();

            }
            else
            {
                Query3 = (from a in Query2
                          where a.NC数量 != 0 && a.ABC数量 != 0
                          select a).ToList();
            }
            var Query4 = (from a in Query3
                           select a).ToList();
            MyQuery = Query4.Count();
            //if (QueryValue == "")
            //{
            //    MyQuery = (from a in db.VABC_NC_StockInSumCost select a).Count();
            //}
            //else
            //{
            //    MyQuery = (from a in db.VABC_NC_StockInSumCost where a.存货编码.Contains(QueryValue) select a).Count();

            //}

            return MyQuery;
        }

        #region 实现翻页功能的代码段
        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {

                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                    case "":
                        pageIndx = 0;
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);

        }
        #endregion
       
    }
}
